package com.antler.smt.admin.service.impl;

import com.antler.smt.admin.mapper.*;
import com.antler.smt.constant.ImgTypeConstant;
import com.antler.smt.entity.*;
import com.antler.smt.response.admin.GetImgResp;
import com.antler.smt.response.base.Result;
import com.antler.smt.response.admin.GetSupplierPictureResp;
import com.antler.smt.service.admin.PictureOpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 后台图片服务实现
 *
 * @author zhangyi
 * @date 2018/5/12 13:30
 */
@Service
public class PictureOpServiceImpl implements PictureOpService {


    @Autowired
    private PurchaserEntityMapper purchaserEntityMapper;

    @Autowired
    private SupplierEntityMapper supplierEntityMapper;

    @Autowired
    private SupplierPictureInfoMapper supplierPictureInfoMapper;

    @Autowired
    private AttachmentEntityMapper attachmentEntityMapper;

    @Autowired
    private RequirementEntityMapper requirementEntityMapper;

    @Autowired
    private MemberEntityMapper memberEntityMapper;

    @Autowired
    private OrderCommentPictureEntityMapper orderCommentPictureEntityMapper;

    @Autowired
    private OrderCommentPictureInfoMapper orderCommentPictureInfoMapper;

    /**
     *  图片分类获取
     *
     * @param id
     * @param type
     * @return
     */
    @Override
    public Result<List<GetImgResp>> getImgInfoList(long id, Integer type) {
        Result result = null;

        //创建存储图片集合链表
        List<GetImgResp> imglist = new ArrayList<>();
        //图片 返回实体
        GetImgResp imgResp = null;

        //采购商图片获取
        if(type.equals(ImgTypeConstant.PURCHASER_TYPE)){

            //采购商实体
            PurchaserEntity purchaserEntity = purchaserEntityMapper.selectByPrimaryKey(id);

            //营业执照
            AttachmentEntity attachmentEntity = attachmentEntityMapper.selectByPrimaryKey(purchaserEntity.getBusinessLicenseAttachmentId());

            imgResp = new GetImgResp();
            imgResp.setImgName(attachmentEntity.getFileName());
            imglist.add(imgResp);
            //服务商图片获取
        }else if(type.equals(ImgTypeConstant.SUPPLIER_TYPE)){
            SupplierEntity supplierEntity = supplierEntityMapper.selectByPrimaryKey(id);

            //银行开户许可证
            AttachmentEntity bankPermit = attachmentEntityMapper.selectByPrimaryKey(supplierEntity.getBankPermitAttachmentId());

            imgResp = new GetImgResp();
            imgResp.setImgName(bankPermit.getFileName());
            imglist.add(imgResp);

            //营业执照
            AttachmentEntity attachmentEntity = attachmentEntityMapper.selectByPrimaryKey(supplierEntity.getBusinessLicenseAttachmentId());

            imgResp = new GetImgResp();
            imgResp.setImgName(attachmentEntity.getFileName());
            imglist.add(imgResp);

            List<GetSupplierPictureResp> getSupplierPictureResps = supplierPictureInfoMapper.selectSupplierImgList(supplierEntity.getId());

            for(int i = 2; i < getSupplierPictureResps.size()+2; i++){
                //图片id
                Long attachmentId = getSupplierPictureResps.get(i - 2).getAttachmentId();
                AttachmentEntity supplierImg = attachmentEntityMapper.selectByPrimaryKey(attachmentId);
                imgResp = new GetImgResp();
                imgResp.setImgName(supplierImg.getFileName());
                imgResp.setType(getSupplierPictureResps.get(i - 2).getType());
                imglist.add(imgResp);
            }

            //发布需求图片获取
        }else if(type.equals(ImgTypeConstant.REQUIREMENT_TYPE)){
            //获取发布需求实体
            RequirementEntity requirementEntity = requirementEntityMapper.selectByPrimaryKey(id);

            //发布需求有4张图片，依次添加
            AttachmentEntity wlGwAttachment = attachmentEntityMapper.selectByPrimaryKey(requirementEntity.getWlGwAttachmentId());
            imgResp = new GetImgResp();
            imgResp.setImgName(wlGwAttachment.getFileName());
            AttachmentEntity bzwjClqdAttachment = attachmentEntityMapper.selectByPrimaryKey(requirementEntity.getBzwjClqdAttachmentId());
            imgResp = new GetImgResp();
            imgResp.setImgName(bzwjClqdAttachment.getFileName());
            imglist.add(imgResp);
            AttachmentEntity bzwjTzAttachment = attachmentEntityMapper.selectByPrimaryKey(requirementEntity.getBzwjTzAttachmentId());
            imgResp = new GetImgResp();
            imgResp.setImgName(bzwjTzAttachment.getFileName());
            imglist.add(imgResp);
            AttachmentEntity bzwjZbwjAttachment = attachmentEntityMapper.selectByPrimaryKey(requirementEntity.getBzwjZbwjAttachmentId());
            imgResp = new GetImgResp();
            imgResp.setImgName(bzwjZbwjAttachment.getFileName());
            imglist.add(imgResp);
            //会员图片获取
        }else if(type.equals(ImgTypeConstant.MEMBER_TYPE)){
            MemberEntity memberEntity = memberEntityMapper.selectByPrimaryKey(id);
            AttachmentEntity attachmentEntity = attachmentEntityMapper.selectByPrimaryKey(memberEntity.getHeadImageAttachmentId());
            imgResp = new GetImgResp();
            imgResp.setImgName(attachmentEntity.getFileName());
            imglist.add(imgResp);
        }else if(type.equals(ImgTypeConstant.ORDER_TYPE)){
            List<OrderCommentPictureEntity> pictureList = orderCommentPictureInfoMapper.getPictureList(id);
            for(OrderCommentPictureEntity o : pictureList){
                AttachmentEntity attachmentEntity = attachmentEntityMapper.selectByPrimaryKey(o.getAttactmentId());
                imgResp = new GetImgResp();
                imgResp.setImgName(attachmentEntity.getFileName());
                imglist.add(imgResp);
            }
        }
        result = new Result(imglist);
        return result;
    }
}
